*** BCG: results for Entry/Exit
* Bustein, Carvalho and Grassi
***




****************************** 1. Preamble ******

* clean up 
	clear all 
	macro drop _all
	set max_memory 50g
	set matsize 11000

*set working directory. Must contain:
	cd "C:\Users\Public\Documents\BCG_DGM\BCG\replication_file_jan25_package\"
	

****************************************CHANGE SPECS AND OUTLIER ***************	
local specsDeta_l   _EntryExit
local winlevel_l    03   
********************************************************************************


	di "===============Quick control================="
	di "specsDeta_l=`specsDeta_l'"
	di "winlevel_l=`winlevel_l'"	
	
	**********************CHOICE of outliuer treatment level
	* winsorization level 
	global winlevel_gl `winlevel_l'
	di "winlevel_gl=$winlevel_gl"

	*trimmeing level (thousands)
	if "`winlevel_l'"=="01"{
		global toptr_gl 990
		global bottr_gl 10
	}
	else if "`winlevel_l'"=="015"{
		global toptr_gl 985
		global bottr_gl 15
	}
	else if "`winlevel_l'"=="02"{
		global toptr_gl 980
		global bottr_gl 20
	}
	else if "`winlevel_l'"=="025"{
		global toptr_gl 975
		global bottr_gl 25
	}
	else if "`winlevel_l'"=="03"{
		global toptr_gl 970
		global bottr_gl 30
	}
	
	di "toptr_gl = $toptr_gl"
	di "toptr_gl = $bottr_gl"

		
	*markup complementaire spec
	global specsDeta `specsDeta_l'
	di "specsDeta (global)==$specsDeta"	
	
	di "results ending in  _$winlevel_gl$specsDeta" 
	
	
	
	*choose the dataset
	global dataset "ficusfare_reduced_9419_sec2_v0222"
	local indlist 08 13 14 15 16 17 18 20 22 23 24 25 26 27 28 29 30 31 32 33 43 46 70 95
	
	
	
	
	di "data=$dataset"
	
	di "==================================="
		

******************************************************************************************
****************************** 2. Load the data ******
******************************************************************************************
	
*select path
cd "C:\Users\Public\Documents\BCG_DGM\BCG\replication_file_jan25_package\"

*load the firm-level data:
	
	
	use data/$dataset, clear 
	
	**drop some useless variables
	capture drop acha1 acha2 acha3 acha5 acha6 cogs cogs_newdef acha1R acha2R acha3R acha5R acha6R  invcorp catotal_tot

	**destring the sector code before the merge
	destring naf2d, replace

	
*Compute Markup 

	** define production variables
	gen s = ln(catotalR)
	gen m = ln(acha4R)	//m will be the variable input 
	gen v = ln(salR)
	gen k = ln(immocorR)
	gen o = ln(autachaR)

	** define cost ratio 
	gen ratio = catotal/acha4 
	
	**Loop over the quantity
	foreach specs in FSQ {
		***merge coefficients 
		merge m:1 naf2d using "data\coefficients_byind_Python_`specs'_baseline.dta", nogen
		
		***rename coefficients 
		foreach beta in const_cd m_cd v_cd o_cd k_cd const_tl m_tl v_tl o_tl k_tl m2_tl mv_tl mo_tl mk_tl v2_tl vo_tl vk_tl o2_tl ok_tl k2_tl {
			rename beta_`beta' beta_`beta'_`specs'
		}
		
		***drop redundant variables
		drop N med_CD med_TL iqr_CD iqr_TL  rho_cd  rho_tl 
		
		
		***calculate elasticity - TL 
		gen elast_tl_s`specs'_t1 = beta_m_tl_`specs'	 				 	 + 	/// 
													2*beta_m2_tl_`specs'*m   + 	///
													  beta_mv_tl_`specs'*v 	 +	///
													  beta_mo_tl_`specs'*o 	 +	///
													  beta_mk_tl_`specs'*k	 
												
		***define elasticity - CD 									
		gen elast_cd_s`specs'_t1 = beta_m_cd_`specs'
		
		
		***calculate markups 
		gen mu_sepcal_TL_s`specs'_t1 =  elast_tl_s`specs'_t1 * ratio 
		gen mu_sepcal_CD_s`specs'_t1 =  elast_cd_s`specs'_t1 * ratio
			
		***calculate log markups
		gen l_mu_sepcal_TL_s`specs'_t1 = log(mu_sepcal_TL_s`specs'_t1) 
		gen l_mu_sepcal_CD_s`specs'_t1 = log(mu_sepcal_CD_s`specs'_t1)
	}
	
	


**drop some useless variables
capture drop ml_* ratio_* elast_*	
	


************************************************************************************************************************************************************************************
******************************************************************* 3. Selecting Final Sample and Some indicators
************************************************************************************************************************************************************************************	
	
*Keep firms with positive VA	
	keep if va>0

*Keep firms with positive catotal and inputs
	/*
	gen sample_firm_inputs = 0
	replace sample_firm_inputs = 1 if (catotalR>0 & salR>0 & immocorR>0 & acha4R>0 & autachaR>0)
	label var sample_firm_inputs "=1 if (catotalR>0 & salR>0 & immocorR>0 & acha4R>0 & autachaR>0)"
	*/
	keep if sample_firm_inputs==1

*Keep the relevent sectors
	keep if sample_sectors2==1 //should be no change
	keep if naf2d_num != 95 & naf2d_num != 70 //Two other sectors that we want to drop
	

*Some indicator of markup level (quantity)
	gen markup_pos = 0
	replace markup_pos = 1 if mu_sepcal_TL_sFSQ_t1 > 0

	gen markup_half = 0
	replace markup_half = 1 if mu_sepcal_TL_sFSQ_t1 > 0.5
	
	gen markup_one = 0
	replace markup_one = 1 if mu_sepcal_TL_sFSQ_t1 > 1


*Keep siren with more than one year
	bysort siren: egen count_siren=count(catotal)
	keep  if count_siren>1
	
	
*Construct Sector-Level VA

	** Sector-level sum of va
	bysort naf_single year: egen va_total = total(va) 
	gen l_va_total = ln(va_total)
	
		
	**Get the list of 5-digits sector to drop
	local Nperiods 26
	preserve
		
		duplicates drop naf_single year , force 	
		keep naf_single year va_total  l_va_total 
		
		bysort naf_single: egen count_naf=count(l_va_total)
		
		duplicates drop naf_single,  force 	
		ta naf_single if count_naf<`Nperiods'
		
		keep if count_naf<`Nperiods'
		
		save  BCG/data/incomplete_naf_single_l_va.dta, replace
		
	restore
	
	
	
*Indicator of sector with complete VA series
	**indicator of bad sectors (manually)
	capture drop sample_sector_5d
	gen sample_sector_5d = 1
	foreach naf_sec in 13.96Z 16.22Z 24.46Z 27.31Z 27.33Z 28.24Z 28.99A 30.12Z 31.09A 33.11Z 33.13Z 33.17Z 33.19Z 33.20B 33.20D 43.11Z 43.21B 46.12A 46.47Z 46.48Z 46.65Z 46.73B{
		replace sample_sector_5d = 0 if naf_single=="`naf_sec'" 
	}
	
	**indicator of bad sectors (systematically)
	merge m:1 naf_single using "BCG/data/incomplete_naf_single_l_va.dta"
	gen sample_sector_5d_v2 = ( _merge!=3 )
	drop _merge count_naf
		
	** Drop obs for these sectors
	ta sample_sector_5d sample_sector_5d_v2
	keep if sample_sector_5d_v2==1
	
	*Get the list of 5-digits sector to drop for positive markup firms
	local Nperiods 26
	preserve
		
		** Sector-level sum of va for positive markup firms
		gen va_pos = .
		replace va_pos = va if markup_pos==1
		
		bysort naf_single year: egen va_pos_total = total(va_pos) 
		gen l_va_pos_total = ln(va_pos_total)
		
		su va_pos va l_va_pos_total l_va_total
		
		** make it a sector level database
		duplicates drop naf_single year , force 	
		keep naf_single year va_pos_total  l_va_pos_total 
		
		bysort naf_single: egen count_naf=count(l_va_pos_total)
		
		duplicates drop naf_single,  force 	
		ta naf_single if count_naf<`Nperiods'
		
		keep if count_naf<`Nperiods'
		
		save  BCG/data/incomplete_naf_single_l_va_pos.dta, replace
		
	restore //one sector to drop beyond the one dropped previously
	
*Indicator of sector with complete VA series for positive markup sample
	**indicator of bad sectors (manually)
	capture drop sample_sector_5d_pos
	gen sample_sector_5d_pos = 1
	foreach naf_sec in 30.30Z{
		replace sample_sector_5d_pos = 0 if naf_single=="`naf_sec'" 
	}
	
	**indicator of bad sectors (systematically)
	merge m:1 naf_single using "BCG/data/incomplete_naf_single_l_va_pos.dta"
	gen sample_sector_5d_pos_v2 = ( _merge!=3 )
	drop _merge count_naf
		
	** Drop obs for these sectors
	ta sample_sector_5d_pos sample_sector_5d_pos_v2
	keep if sample_sector_5d_pos_v2==1
	

*Aggregate-level sum of va
	**For the full sample
	bysort year: egen va_agg = total(va)
	gen l_va_agg = ln(va_agg)
	
		

*****************************************************************************************************************************************************************
******************************************************************* 4. Construct Regression Variables
*****************************************************************************************************************************************************************	
		
		
* Sector-Level VA (at naf_single level)
local Nperiods 26
preserve
	
	**Make the data a sector-level panel
	duplicates drop naf_single year , force 	
	keep naf_single year va_total l_va_total 

	**set up panel
	egen naf_single_num = group(naf_single)
	xtset naf_single_num year
	
	**fix gaps (hp filter does not run with gaps but now it should no be any of these gaps)
	drop if l_va_total == .  //0 changes
	bysort naf_single: drop if _N < `Nperiods' //0 changes

	** generate HP-filtered data 
	tsfilter hp x = l_va_total, smooth(6.25) trend(l_va_total_hp)
	drop x
	gen l_va_total_hp_dev = l_va_total - l_va_total_hp
	
	** generate Hamilton-filtered data 
	hamiltonfilter l_va_total, frequency(yearly) stub(l_va_total_ha) 
	gen l_va_total_ha_dev = l_va_total - l_va_total_ha_trend
	
	** generate First-Difference-filtered data 
	xtset naf_single_num year
	gen  l_va_total_FD = l_va_total - L.l_va_total
	
	** generate Demean data 
	by naf_single_num: egen l_va_total_mean = mean(l_va_total)
	gen  l_va_total_demean = l_va_total - l_va_total_mean	

	* save results 
	save BCG/data/l_va_total_hp_$specsDeta, replace

restore


* Aggregate-Level VA 
preserve
	**Make the data a yearly time serie
	duplicates drop year , force 	
	keep year va_agg l_va_agg  
	**Set up time serie
	tset year 
	
	**demean aggregate GDP
	egen l_va_agg_mean = mean(l_va_agg)
	gen l_va_agg_demean = l_va_agg - l_va_agg_mean
	
	**FD of GDP
	gen FD_l_va_agg = l_va_agg - L.l_va_agg

	**generate HP-filtered data for full sample
	tsfilter hp x = l_va_agg, smooth(6.25) trend(l_va_agg_hp)
	drop x
	gen l_va_agg_hp_dev = l_va_agg - l_va_agg_hp
	
	
	** generate Hamilton-filtered data 
	hamiltonfilter l_va_agg, frequency(yearly) stub(l_va_agg_ha) 
	gen l_va_agg_ha_dev = l_va_agg - l_va_agg_ha_trend
	
	
	**save results 
	save BCG/data/l_va_agg_hp_$specsDeta, replace

restore



*Merging with sector-level HP-filtered data for all firms
	merge m:1 naf_single year using "BCG/data/l_va_total_hp_$specsDeta.dta" 
	drop _merge 
	
*Merging with aggregate level HP-filtered data
	merge m:1 year using "BCG/data/l_va_agg_hp_$specsDeta.dta" 
	drop _merge 


*Market Share
	
	**Sector level revenue
	bysort naf_single year: egen catotal_total = total(catotal)
	
	**Market Share
	gen share5 = catotal/catotal_total
	
	**Some summary stats
	su share5  


*Interaction market share with sector output	
	**HP-Deviation
		***Interaction market share * sector output: (market share * HP-filtered (log) sector-level VA)
			gen l_va_total_hp_d_inter = l_va_total_hp_dev*share5	 
			
		
	** Hamilton-filter
		***Interaction market share * sector output: (market share * HA-filtered (log) sector-level VA)
			gen l_va_total_ha_d_inter = l_va_total_ha_dev*share5	 
	
	**First-Difference (log)
		***Interaction market share * sector output: (market share * FD(log) sector-level VA)
			gen l_va_total_FD_d_inter = l_va_total_FD*share5	 
			
	
	**Demeaned
		***Interaction market share * sector output: (market share * Demean (log) sector-level VA)
			gen l_va_total_demean_d_inter = l_va_total_demean*share5	 
			
	
	
*Revenue Share in total revenue
	
	**Yearly total revenue
	bysort  year: egen catotal_aggr = total(catotal)
	
	**Revenue Share
	gen revShare = catotal/catotal_aggr
	
	**Some summary stats
	su revShare  

	
	

*Inverse markup
	xtset firmsId year

	**level
	gen inv_mu_sepcal_TL_sFSQ_t1 = 1/mu_sepcal_TL_sFSQ_t1
			
	**in first-diff			
	gen FD_inv_mu_sepcal_TL_sFSQ_t1  = inv_mu_sepcal_TL_sFSQ_t1 - L.inv_mu_sepcal_TL_sFSQ_t1
	


*Compute number of firms in each 5digit sectors
	bysort naf_single year: egen firmcount_naf_va=count(va)
	bysort naf_single year: egen firmcount_naf_ca=count(catotal)
	bysort naf_single year: egen firmcount_naf_mu=count(mu_sepcal_TL_sFSQ_t1)
	su firmcount_naf_*
	
	*since all the same let us keep only one
	drop firmcount_naf_ca firmcount_naf_mu
	*rename  firmcount_naf_ca firmcount_naf
	
***************************************************************************************************************************************************************	
*************** Entry-Exit 
*****************************************************************************************************************************************************************

	*** Check the number of firms per year
	ta year, su(emp)
	egen Nb_siren_year = count(siren), by(year)
	ta year, su(Nb_siren_year)

	*** count firms that enter or exit 
	bysort siren: egen minyear = min(year)
	bysort siren: egen maxyear = max(year) 


	*** Some other variables
	bysort siren: gen minyear_plus1 = minyear+1
	bysort siren: gen maxyear_minus1 = maxyear-1

	

	*** Construct indicator
		*****entrant
			*entrant indicator
			gen entrant = 0
			bysort siren year: replace entrant = 1 if minyear==year 

			gen entrant_1year = 0
			bysort siren year: replace entrant_1year = 1 if minyear_plus1==year 
			
			
		*****exit
			*exiters indicator
			gen exiter = 0
			bysort siren year: replace exiter = 1 if maxyear==year 
			
			gen exiter_1year = 0
			bysort siren year: replace exiter_1year = 1 if maxyear_minus1==year 
			

	
	*** Some results on contribution of entrant/exit
		*total number of entrant and exiters
		bysort year: egen nb_entry = total(entrant)
		bysort year: egen nb_exit = total(exiter)

		*entry and exit rate
		gen entry_rate = nb_entry/Nb_siren_year
		gen exit_rate = nb_exit/Nb_siren_year

		su entry_rate if year ~=1994
		su exit_rate if year ~=2019

		ta year, su(entry_rate)
		ta year, su(exit_rate)

		

		**Contribution to Sales
			***entrant share in total catotal
			bysort year: egen entry_catotal = total(catotal) if entrant==1
			bysort year: gen entry_share_catotal = entry_catotal/catotal_aggr
			
			
			***exit share in total catotal
			bysort year: egen exit_catotal = total(catotal) if exiter==1
			bysort year: gen exit_share_catotal = exit_catotal/catotal_aggr
			
			***stats
			su entry_share_catotal  if year~=1994
			ta year if year~=1994, su(entry_share_catotal  ) 
			
			su exit_share_catotal  if year~=2019
			ta year if year~=2019, su(exit_share_catotal  ) 
			
			****Robustness first-year effect
				***entrant share in total catotal
				bysort year: egen entry_p1_catotal = total(catotal) if entrant_1year==1
				bysort year: gen entry_p1_share_catotal = entry_catotal/catotal_aggr
				
				
				***exit share in total catotal
				bysort year: egen exit_m1_catotal = total(catotal) if exiter_1year==1
				bysort year: gen exit_m1_share_catotal = exit_catotal/catotal_aggr
				
				***stats
				su entry_p1_share_catotal  if year>1995
				ta year if year>1995, su(entry_p1_share_catotal  ) 
				
				su exit_m1_share_catotal  if year<2018
				ta year if year<2018, su(exit_m1_share_catotal  ) 
			
		


**************************************************************************************************************************************************************
******************************************************************* 5. Firm-Level Regressions
**************************************************************************************************************************************************************


		
******************************************************************************************		
*************** 5.1 inverse markup on market share 


	**Winsorized the LHS
		local top_tr $toptr_gl
		local bot_tr $bottr_gl
		local winlevel $winlevel_gl
		
		gen mu_sepcal_TL_sFSQ_t1_pos = .
		replace mu_sepcal_TL_sFSQ_t1_pos = mu_sepcal_TL_sFSQ_t1 if markup_pos==1
		
		gen inv_mu_sepcal_TL_sFSQ_t1_pos = .
		replace inv_mu_sepcal_TL_sFSQ_t1_pos = inv_mu_sepcal_TL_sFSQ_t1 if markup_pos==1
		
		
		winsor  inv_mu_sepcal_TL_sFSQ_t1_pos , p(.`winlevel') gen(inv_mu_sepcal_TL_sFSQ_t1_pos_w)
		
	**Some summarry stats
		su inv_mu_sepcal_TL_sFSQ_t1_pos_w  share5 if markup_pos==1
		
		
	**Regression
		
			****In first-difference
			
					*****Compute the LHS and RHS (and winsorized)
						******set up panel
						xtset firmsId year
						
						******Compute First-Difference
						gen FDinv_mu_sepcal_TL_sFSQ_t1_pos = inv_mu_sepcal_TL_sFSQ_t1_pos - L.inv_mu_sepcal_TL_sFSQ_t1_pos
						gen FDshare5 = share5 - L.share5
						
						local top_tr $toptr_gl
						local bot_tr $bottr_gl
						local winlevel $winlevel_gl
										
						winsor  FDinv_mu_sepcal_TL_sFSQ_t1_pos , p(.`winlevel') gen(FDinv_mu_sepcal_TL_sFSQ_t1_pos_w)

		
				
					
					****Condition on entrant/exit
					 gen entrant_1year_or_exiter = 0
					 replace entrant_1year_or_exiter =1 if entrant_1year==1
					 replace entrant_1year_or_exiter =1 if exiter==1
					 ta entrant_1year_or_exiter if markup_pos==1 & year>1994 & FDinv_mu_sepcal_TL_sFSQ_t1_pos_w~=. &  FDshare5~=.
						
					
					*****Regressions
						*with no FE
						reghdfe FDinv_mu_sepcal_TL_sFSQ_t1_pos_w  FDshare5  if markup_pos==1 & year>1994 & entrant_1year_or_exiter==0, noabsorb cl(year firmsId) 
						outreg2 using BCG\results_sl\MarkupMShare_FD_$winlevel_gl$specsDeta , replace tex   keep( FDshare5 ) addtext(Firm FE, No, Year FE, No, Cluster , Firm Year)
										
				
					
	
******************************************************************************************
*************** 5.2 Firm markup on sector output   
	**Sorting the data
		xtset firmsId year

		
	**Taking First-Difference
		gen FD_l_mu_sepcal_TL_sFSQ_t1 = l_mu_sepcal_TL_sFSQ_t1 - L.l_mu_sepcal_TL_sFSQ_t1

	**Winsorized the LHS
		local top_tr $toptr_gl
		local bot_tr $bottr_gl
		local winlevel $winlevel_gl
				
		winsor  l_mu_sepcal_TL_sFSQ_t1 , p(.`winlevel') gen(l_mu_sepcal_TL_sFSQ_t1_w)
		winsor  FD_l_mu_sepcal_TL_sFSQ_t1 , p(.`winlevel') gen(FD_l_mu_sepcal_TL_sFSQ_t1_w)
		
				
		**Regressions for specification in FD
			ta entrant_1year_or_exiter if markup_pos==1 & year>1994 & FDinv_mu_sepcal_TL_sFSQ_t1_pos_w~=. &  FD_l_mu_sepcal_TL_sFSQ_t1_w~=. & l_va_total_FD~=. & l_va_total_FD_d_inter~=.
		
		
			
			***** without year FE 
			reghdfe FD_l_mu_sepcal_TL_sFSQ_t1_w  l_va_total_FD l_va_total_FD_d_inter  if markup_pos==1 & year>1994  & entrant_1year_or_exiter==0, noabsorb  cl(naf_single_num#year)
			outreg2 using BCG\results_sl\MarkupSectorOutput_$winlevel_gl$specsDeta , append tex   keep( l_va_total_FD l_va_total_FD_d_inter) addtext(Firm FE, No, Year FE, No, Cluster , Sector * Year, Sample, Pos, RHS, Max, Weight, No, Drop 94, Yes)
			*



	
**************************************************************************************************************************************************************
******************************************************************* 6. Check that sector-level variables and results are robust to entry/exit
***************************************************************************************************************************************************************

******************************************************************************
***** 6.1 Construct some sector-level variables

	***** 6.1.1 **HHI
		*For full sample
			gen share5_sq = share5*share5
			bysort year naf_single: egen hhi = total(share5_sq), missing


			**in logs
			gen l_hhi = log(hhi)
			

		**Declare the panel
		xtset firmsId year
				

			
	***** 6.1.2 **Sector Level Markup
					
		*With winsorized inverse markup	and market share on full sample	
			
			bysort year naf_single: egen inv_mu_TL_sFSQ_t1_pos_w_h = wtmean(inv_mu_sepcal_TL_sFSQ_t1_pos_w), weight( share5 )
			
			gen mu_TL_sFSQ_t1_pos_w_h = 1/inv_mu_TL_sFSQ_t1_pos_w_h
			gen  l_mu_TL_sFSQ_t1_pos_w_h = log(mu_TL_sFSQ_t1_pos_w_h)
		
		
		
			
		** For continuing firms and summing to one aggregation
			xtset firmsId year
			

			*continuing firm indicator
			gen CON = 0
			replace CON = 1 if l.inv_mu_sepcal_TL_sFSQ_t1_pos_w~=. & inv_mu_sepcal_TL_sFSQ_t1_pos_w~=.
			
			*sum of revenue for continuing firms
			gen temp = catotal*CON
			bysort naf_single year: egen catotal_total_CON = total(temp)
			drop temp
			
			*computing sector level markup
			xtset firmsId year
			
			gen temp  = inv_mu_sepcal_TL_sFSQ_t1_pos_w*catotal/catotal_total_CON 
			
			gen temp_CON = .
			gen temp_CON_lag = .
			
			replace temp_CON = temp if l.temp~=. & temp~=.
			replace temp_CON_lag = l.temp if l.temp~=. & temp~=.
			
			gen FD_temp_CON = temp_CON - temp_CON_lag
			
			bysort naf_single year: egen inv_mu_TL_sFSQ_t1_pos_w_CON = total(temp_CON) 
			bysort naf_single year: egen inv_mu_TL_sFSQ_t1_pos_w_CON_lag = total(temp_CON_lag) 
			
			bysort naf_single year: egen FD_inv_mu_TL_sFSQ_t1_pos_w_CON = total(FD_temp_CON) 
			
			drop temp temp_CON temp_CON_lag
			
			gen mu_TL_sFSQ_t1_pos_w_CON = 1/inv_mu_TL_sFSQ_t1_pos_w_CON
			gen mu_TL_sFSQ_t1_pos_w_CON_lag = 1/inv_mu_TL_sFSQ_t1_pos_w_CON_lag
			
			gen  l_mu_TL_sFSQ_t1_pos_w_CON = log(mu_TL_sFSQ_t1_pos_w_CON)
			gen  l_mu_TL_sFSQ_t1_pos_w_CON_lag = log(mu_TL_sFSQ_t1_pos_w_CON_lag)
			
			
		
		
******************************************************************************************
*save the firm-level database
save BCG/data/full_firm_temp.dta, replace
******************************************************************************************
	
		
******************************************************************************		
****** 6.3	**Compare sector-level data with baseline value (and compute some FD measure)



******************************************************************************	
***** SECTOR-LEVEL PANEL ****
*Keep one observation by sector*year
duplicates drop naf_single_num  year, force 

*save/USE the data
	save BCG/data/full_sec$specsDeta_${winlevel_gl}.dta, replace
	
	use BCG/data/full_sec$specsDeta_${winlevel_gl}.dta, clear

*set up panel
xtset naf_single_num  year
******************************************************************************	

		**Take some first diff
			gen FDhhi = hhi - L.hhi
			gen FD_l_hhi = l_hhi-l.l_hhi
			
			gen FD_l_mu_TL_sFSQ_t1_pos_w_h = d.l_mu_TL_sFSQ_t1_pos_w_h
			
			gen FDinv_mu_TL_sFSQ_t1_pos_w_h = inv_mu_TL_sFSQ_t1_pos_w_h - L.inv_mu_TL_sFSQ_t1_pos_w_h
			gen FD_l_mu_TL_sFSQ_t1_pos_w_CON = l_mu_TL_sFSQ_t1_pos_w_CON - l_mu_TL_sFSQ_t1_pos_w_CON_lag
			
		
		
			
		**Descriptive stats

			
			***(log) Sector Markup
			su FD_l_mu_TL_sFSQ_t1_pos_w_h FD_l_mu_TL_sFSQ_t1_pos_w_CON
			

			
			***(inv) Sector Markup
			su FDinv_mu_TL_sFSQ_t1_pos_w_h FD_inv_mu_TL_sFSQ_t1_pos_w_CON
			

			

			

	

******************************************************************************
**** 6.4 Markup HHI regression
		
		******set up panel
		xtset naf_single_num year
					

			*******Regressions

					*baseline (no entry/exit)
					reghdfe FDinv_mu_TL_sFSQ_t1_pos_w_h  FDhhi  if year>1994   , noabsorb  cl(year naf_single_num)
					outreg2 using BCG\results_sl\MarkupHhi_FD_$winlevel_gl$specsDeta , replace tex   keep(FDhhi) addtext(Sector FE, No, Year FE, No, Cluster , Sector Year , Specs, Baseline)
				
					
					*with entry_exit
					reghdfe FD_inv_mu_TL_sFSQ_t1_pos_w_CON  FDhhi  if year>1994   , noabsorb  cl(year naf_single_num)
					outreg2 using BCG\results_sl\MarkupHhi_FD_$winlevel_gl$specsDeta , append tex   keep(FDhhi) addtext(Sector FE, No, Year FE, No, Cluster , Sector Year , Specs, Entry-Exit)
					
					
			
				
				
******************************************************************************			
***** 6.5 Sector-level Markup and Sector Output 

	*baseline (no entry/exit)
	reghdfe d.l_mu_TL_sFSQ_t1_pos_w_h  d.l_va_total   i.year  if year>1994, absorb(naf_single_num)  cl(naf_single_num )
	outreg2 using BCG\results_sl\SectorMarkup_SectorOutput_$winlevel_gl$specsDeta , replace tex   keep(d.l_va_total) addtext(Sector FE, Yes, Year FE, Yes, Cluster , Sector  Year, specs, Baseline)
	
	
	*with entry_exit
	reghdfe FD_l_mu_TL_sFSQ_t1_pos_w_CON   l_va_total_FD   i.year  if year>1994, absorb(naf_single_num)  cl(naf_single_num )
	outreg2 using BCG\results_sl\SectorMarkup_SectorOutput_$winlevel_gl$specsDeta , append tex   keep(l_va_total_FD) addtext(Sector FE, Yes, Year FE, Yes, Cluster , Sector  Year,  specs, Entry-Exit)
	
	
	

	
******************************************************************************			
***** 6.7 Sector-level Markup - Aggregate-Output 

	*Weight variable
	bysort naf_single_num: egen weightvar_va_total = mean(va_total)

	**sector cluster
	*baseline
	reghdfe FD_l_mu_TL_sFSQ_t1_pos_w_h  FD_l_va_agg [aweight=weightvar_va_total] if year>1994 , absorb(naf_single_num)  cl(year)
		outreg2 using BCG\results_sl\SectorMarkup_AggregateOutput_$winlevel_gl$specsDeta , replace tex   keep(FD_l_va_agg ) addtext(Sector FE, Yes, Cluster , Year,  Specs, Baseline)
	
	*with entry-exit
	reghdfe FD_l_mu_TL_sFSQ_t1_pos_w_CON  FD_l_va_agg [aweight=weightvar_va_total] if year>1994 , absorb(naf_single_num)  cl(year)
	outreg2 using BCG\results_sl\SectorMarkup_AggregateOutput_$winlevel_gl$specsDeta , append tex   keep(FD_l_va_agg ) addtext(Sector FE, Yes, Cluster , Year, Specs, CON on Baseline)
	
	**sector cluster (Baseline)
	*baseline
	reghdfe FD_l_mu_TL_sFSQ_t1_pos_w_h  FD_l_va_agg [aweight=weightvar_va_total] if year>1994 , absorb(naf_single_num)  cl(naf_single_num)
		outreg2 using BCG\results_sl\SectorMarkup_AggregateOutput_$winlevel_gl$specsDeta , replace tex   keep(FD_l_va_agg ) addtext(Sector FE, Yes, Cluster , Sector,  Specs, Baseline)
	
	*with entry-exit
	reghdfe FD_l_mu_TL_sFSQ_t1_pos_w_CON  FD_l_va_agg [aweight=weightvar_va_total] if year>1994 , absorb(naf_single_num)  cl(naf_single_num)
	outreg2 using BCG\results_sl\SectorMarkup_AggregateOutput_$winlevel_gl$specsDeta , append tex   keep(FD_l_va_agg ) addtext(Sector FE, Yes, Cluster , Sector,  Specs, CON on Baseline)
	
	
	
	
	
	